package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.GxDept;
import com.ruoyi.system.service.IGxDeptService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.system.domain.GxAssess;
import com.ruoyi.system.service.IGxAssessService;

/**
 * 部门Controller
 *
 * @author ruoyi
 * @date 2023-01-14
 */
@RestController
@RequestMapping("/system/dept")
public class GxDeptController extends BaseController
{
    @Autowired
    private IGxDeptService gxDeptService;

    @Autowired
    private IGxAssessService assessService;

    /**
     * 查询部门列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GxDept gxDept)
    {
        startPage();
        List<GxDept> list = gxDeptService.selectGxDeptList(gxDept);
        return getDataTable(list);
    }

    /**
     * 导出部门列表
     */
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxDept gxDept)
    {
        List<GxDept> list = gxDeptService.selectGxDeptList(gxDept);
        ExcelUtil<GxDept> util = new ExcelUtil<GxDept>(GxDept.class);
        util.exportExcel(response, list, "部门数据");
    }

    @GetMapping("/authAssess/{deptId}")
    public AjaxResult authAssess(@PathVariable("deptId") Long deptId)
    {
        AjaxResult ajax = AjaxResult.success();
        GxDept dept = gxDeptService.selectGxDeptByDeptId(deptId);
        List<GxAssess> assesses = assessService.selectAssessesByDeptId(deptId);
        ajax.put("dept", dept);
        ajax.put("assesses", assesses);
        return ajax;
    }

    @PutMapping("/authAssess")
    public AjaxResult insertAuthAssess(Long deptId, Long[] assessIds)
    {
//        userService.checkUserDataScope(userId);
        gxDeptService.insertDeptAuthAssess(deptId, assessIds);
        return success();
    }


    /**
     * 获取部门详细信息
     */
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId)
    {
        return AjaxResult.success(gxDeptService.selectGxDeptByDeptId(deptId));
    }

    /**
     * 加载对应角色部门列表树
     */
    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<GxDept> depts = gxDeptService.selectGxDeptList(new GxDept());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", gxDeptService.selectDeptListByRoleId(roleId));
//        ajax.put("depts", gxDeptService.buildDeptTreeSelect(depts));
        return ajax;
    }

    /**
     * 新增部门
     */
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxDept gxDept)
    {
        return toAjax(gxDeptService.insertGxDept(gxDept));
    }

    /**
     * 修改部门
     */
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxDept gxDept)
    {
        return toAjax(gxDeptService.updateGxDept(gxDept));
    }

    /**
     * 删除部门
     */
    @Log(title = "部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(gxDeptService.deleteGxDeptByDeptIds(deptIds));
    }
}
